﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace MassIntellect.WorkIt
{
    public class CrossPipe
    {
        [Serialize(SerializeAs.Attribute, IgnoreDefault = true)]
        public virtual string Name { get; set; }

        public string Mnemo
        {
            get { return output + '>' + input; }
            set
            {
                int i = value.IndexOf('>');
                if ((i >= 0).IfFalse(() => new Exception("В мнемокоде канала не указан символ '>'")))
                {
                    output = value.Substring(0, i);
                    input = value.Substring(i + 1);
                }
            }
        }

        [Serialize(SerializeAs.Attribute)]
        public string output
        {
            get
            {
                return outName + '.' + outPipe;
            }
            set
            {
                int i = value.IndexOf('.');
                if (i >= 0)
                {
                    outName = value.Substring(0, i);
                    if (i < value.Length - 1)
                        outPipe = value.Substring(i + 1);
                    else
                        outPipe = "";
                }
            }
        }

        [Serialize(SerializeAs.Attribute)]
        public string input
        {
            get
            {
                return inName + '.' + inPipe;
            }
            set
            {
                int i = value.IndexOf('.');
                if (i >= 0)
                {
                    inName = value.Substring(0, i);
                    if (i < value.Length - 1)
                        inPipe = value.Substring(i + 1);
                    else
                        inPipe = "";
                }
            }
        }

        public string inName;

        public string inPipe;

        public string outName;

        public string outPipe;

        public IPipe InPipe
        {
            get
            {
                return _InPipe ?? Pipe.Null;
            }
            set
            {
                _InPipe = value;
            }
        }
        private IPipe _InPipe;

        public IPipe OutPipe
        {
            get
            {
                return _OutPipe ?? Pipe.Null;
            }
            set
            {
                _OutPipe = value;
            }
        }
        private IPipe _OutPipe;

        [Serialize(SerializeAs.Attribute)]
        public virtual bool IsActive { get; set; }

        public bool IsConnected { get; private set; }

        public virtual void Activate()
        {

        }

        public void Connect()
        {
            if (BeforeConnect())
                AfterConnect();
        }

        protected virtual bool BeforeConnect()
        {
            OutPipe.OnPulse += InPipe.Pulse;
            return true;
        }

        protected virtual void AfterConnect()
        {
            IsConnected = true;
        }

        public void Disconnect()
        {
            if (BeforeDisconnect())
                AfterDisconnect();
        }

        protected virtual bool BeforeDisconnect()
        {
            OutPipe.OnPulse -= InPipe.Pulse;
            return true;
        }

        protected virtual void AfterDisconnect()
        {
            IsConnected = false;
        }

        public override string ToString()
        {
            return Mnemo;
        }
    }


}
